package com.cn.controller;

import com.cn.Mapper.goodsMapper.OrderMapper;
import com.cn.service.GoodsService.CartService;
import com.cn.service.GoodsService.HotsService;
import com.cn.service.GoodsService.OrderService;
import com.cn.service.ShopService.InventoryService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName CartController
 * @Description TODO
 * @Author yujiajing
 * @Date 2022/10/12 20:25
 * @Version 1.0
 */

/**
 * @request:服务器向控制层发出的请求信息
 * @response：控制层对服务器做出的反应
 * @RequestParam
 *
 */
@Controller
@RequestMapping("/Cart")
public class CartController {
    @Autowired
    CartService cartService;
    OrderService orderService;
    HotsService hotsService;
    InventoryService inventoryService;
    /**
     * 1.控制层接收Ajax传来的Goods_cart对象
     * 2.控制层为对象赋值
     */
    @RequestMapping(value = "/addGoods",method = RequestMethod.POST)
    public void addGoods(@Param("id") String id ){
        System.out.println("添加商品");

        cartService.InsertCartByAuto(id);
        cartService.analyseSubtotal(id);

    }
    @RequestMapping(value = "empty",method = RequestMethod.POST)
    @ResponseBody
    public  void  emptyCart(){

        //添加订单数据
        OrderService orderService=new OrderService();
        orderService.insertOrderByCartId();
        System.out.println("添加订单成功！");
        //更新商品库存
        InventoryService inventoryService=new InventoryService();
        inventoryService.updateInventoryByCart();
        System.out.println("更新库存成功！");
        //更新热力值
        HotsService hotsService=new HotsService();
        hotsService.updateHotsByCart();
        System.out.println("更新热力值成功！");
        //清空购物车
        CartService cartService=new CartService();
        cartService.EmptyCart();
        System.out.println("清空购物车成功！");



    }
    @RequestMapping(value = "/deleteOne" ,method =RequestMethod.POST )
    @ResponseBody
    public void deleteCartById(@RequestBody String goods_id){
        String RealId=goods_id.substring(3);
        System.out.println(RealId);
     /*  CartService cartService=new CartService();*/
       cartService.deleteCartById(RealId);
    }
    @RequestMapping(value = "/addNumber" ,method =RequestMethod.POST )
    @ResponseBody
    public void autoaddOne(@RequestBody String goods_id){
        String RealId=goods_id.substring(3);
        System.out.println(RealId);
        System.out.println(RealId);
       /* CartService cartService=new CartService();*/
        //商品数目+1
        cartService.autoAddOneForNumber(RealId);
        //重新计算小计
        cartService.analyseSubtotal(RealId);
    }
}
